#!/bin/bash

# 安装脚本 - 机械臂语音控制守护进程

set -e

echo "🤖 开始安装机械臂语音控制守护进程..."

# 检查Python3
if ! command -v python3 &> /dev/null; then
    echo "❌ 未找到 Python3，请先安装 Python3"
    exit 1
fi

# 检查依赖
echo "📦 检查系统依赖..."
for pkg in python3-pip alsa-utils sox; do
    if ! dpkg -l | grep -q $pkg; then
        echo "📥 安装 $pkg..."
        sudo apt-get update
        sudo apt-get install -y $pkg
    fi
done

# 安装Python依赖
echo "🐍 安装Python依赖..."
pip3 install vosk requests numpy

# 创建必要的目录
echo "📁 创建目录结构..."
sudo mkdir -p /var/log/robotic_arm
sudo chown $USER:$USER /var/log/robotic_arm

# 复制服务文件
echo "🔧 配置系统服务..."
sudo cp robotic_arm.service /etc/systemd/system/
sudo systemctl daemon-reload

# 设置权限
echo "🔒 设置文件权限..."
chmod +x robotic_arm_daemon.py
chmod +x install_daemon.sh

# 启用服务
echo "🚀 启用守护进程..."
sudo systemctl enable robotic-arm.service

echo "✅ 安装完成！"
echo ""
echo "📋 使用命令:"
echo "启动服务: sudo systemctl start robotic-arm"
echo "停止服务: sudo systemctl stop robotic-arm"
echo "查看状态: sudo systemctl status robotic-arm"
echo "查看日志: sudo journalctl -u robotic-arm -f"
echo ""
echo "🔧 服务将在系统启动时自动运行"